//
//  MyViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/21.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "MyViewModel.h"
#import "MyOrderTypeTableViewCell.h"
#import "MyOtherTableViewCell.h"
#import "MyRoleTableViewCell.h"
#import "MyHeaderView.h"
#import "MyOrderTableViewHeaderFooterView.h"
#import "MyListOtherTableViewHeaderFooterView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "MyFreeOrderTableViewCell.h"
#import "MyFreeSixSelectTableViewCell.h"
#import "LoginModel.h"

@interface MyViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    MyHeaderView * headerView;
}
@end

@implementation MyViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        CGFloat navHeight;
        if (iPhoneX) {
            navHeight = 171-88;
        }else{
            navHeight = 113-64;
        }
        tableView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - navHeight);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = [self createHeaderView];
        [tableView registerClass:[MyOrderTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"MyOrderTableViewHeaderFooterView"];
        [tableView registerClass:[MyListOtherTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"MyListOtherTableViewHeaderFooterView"];
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [superView addSubview:tableView];
        //
        self.upDataInforSbuject = [RACSubject subject];
        @weakify(self)
        [self.upDataInforSbuject subscribeNext:^(id  _Nullable x) {
            @strongify(self)
            if (self.userInforData.length>0) {
                LoginModel * l_Modle = [NSKeyedUnarchiver unarchiveObjectWithData:self.userInforData];
                self->headerView.infor_model = l_Modle;
            }
        }];
    }
    return self;
}

-(UIView *)createHeaderView{
    headerView = [[MyHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(198))];
    kWeakSelf(self);
    headerView.Block_HeaderAcyion = ^(GLGWButton * _Nullable sender) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(headerAction:)]) {
            [weakself.delegate headerAction:sender];
        }
    };
    return headerView;
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight;
    if (indexPath.section == 3) {
        cellHeight = kHeightProportion_In_iPhoneTriploid(55);
    }else{
        cellHeight = kHeightProportion_In_iPhoneTriploid(88);
        if (indexPath.section == 2 && self.accountType == AccountType_FreeManagers) {
            cellHeight = kHeightProportion_In_iPhoneTriploid(167);
        }
    }
    return cellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (indexPath.section) {
        case 0:{
            MyOrderTypeTableViewCell * cell_order = [MyOrderTypeTableViewCell cellWithTableView:tableView];
            [cell_order setBlock_OrderTypeAction:^(GLGWButton * _Nullable orderBut) {
                if (self.delegate && [self.delegate respondsToSelector:@selector(orderTypeSelectAction:)]) {
                    [self.delegate orderTypeSelectAction:orderBut];
                }
            }];
            cell = cell_order;
        }
            break;
        case 1:
        case 2:{
            if (self.accountType == AccountType_FreeManagers) {//自由经理人
                if (indexPath.section == 1) {
                    MyFreeOrderTableViewCell * cell_freeOrder = [MyFreeOrderTableViewCell cellWithTableView:tableView];
                    cell_freeOrder.Block_FreeFourAction = ^(GLGWButton * _Nonnull f_but) {
                        if (self.delegate && [self.delegate respondsToSelector:@selector(selectIndexSection:theSelectBut:)]) {
                            [self.delegate selectIndexSection:indexPath theSelectBut:f_but];
                        }
                    };
                    cell = cell_freeOrder;
                }else{
                    MyFreeSixSelectTableViewCell * cell_Six = [MyFreeSixSelectTableViewCell cellWithTableView:tableView];
                    cell_Six.Block_MySeverInfor = ^(GLGWButton * _Nonnull inforBut) {
                        if (self.delegate && [self.delegate respondsToSelector:@selector(selectIndexSection:theSelectBut:)]) {
                            [self.delegate selectIndexSection:indexPath theSelectBut:inforBut];
                        }
                    };
                    cell = cell_Six;
                }
            }else{
                MyOtherTableViewCell * cell_other = [tableView dequeueReusableCellWithIdentifier:@"MyOtherTableViewCell"];
                if (!cell_other) {
                    cell_other = [[MyOtherTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyOtherTableViewCell" theSection:indexPath.section];
                }
                cell_other.Block_SeleButAction = ^(GLGWButton * _Nullable sender) {
                    if (self.delegate && [self.delegate respondsToSelector:@selector(selectIndexSection:theSelectBut:)]) {
                        [self.delegate selectIndexSection:indexPath theSelectBut:sender];
                    }
                };
                cell = cell_other;
            }
            
        }
            break;
        case 3:{
            MyRoleTableViewCell * cell_role = [MyRoleTableViewCell cellWithTableView:tableView];
            cell = cell_role;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView * headerView;
    if (section == 0) {
        MyOrderTableViewHeaderFooterView * headerView_Order = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyOrderTableViewHeaderFooterView"];
        headerView_Order.Block_OrderAction = ^{
            if (self.delegate && [self.delegate respondsToSelector:@selector(orderPushAction)]) {
                [self.delegate orderPushAction];
            }
        };
        headerView = headerView_Order;
    }else{
        MyListOtherTableViewHeaderFooterView * headerView_other = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyListOtherTableViewHeaderFooterView"];
        headerView = headerView_other;
    }
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return kHeightProportion_In_iPhoneTriploid(32);
    }else{
        return kHeightProportion_In_iPhoneTriploid(10);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NoDataFootTableViewHeaderFooterView * footView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(roleDidSelectRowAtIndexPath:)]) {
        [self.delegate roleDidSelectRowAtIndexPath:indexPath];
    }
}

@end

